package com.autocar.reader.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.autocar.reader.R;
import com.autocar.reader.bean.ProductSubDetailBean;
import com.autocar.reader.net.ImageLoader;
import com.autocar.reader.net.ImageLoader.ImageCallback;
import com.autocar.reader.util.ConstantData;
import com.autocar.reader.util.ImageUtils;
import com.autocar.reader.view.XListView;
import com.org.any.autocar.fragment.IopenProductDetail;

public class ProductSubDetailAdapter extends BaseAdapter implements
		ImageCallback {

	private LayoutInflater inflater;
	private List<ProductSubDetailBean> listnews;
	private XListView rootView;
	private ImageLoader load;
	private Context context;
	private IopenProductDetail iopen;

	public ProductSubDetailAdapter(Context context, XListView rootView,
			IopenProductDetail iopen) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.rootView = rootView;
		this.iopen = iopen;
		load = new ImageLoader(this);
	}

	public void setListItems(List<ProductSubDetailBean> listnews) {
		this.listnews = listnews;
		notifyDataSetChanged();
	}

	public List<ProductSubDetailBean> getListItems() {
		return listnews;
	}

	@Override
	public int getCount() {
		return (listnews == null) ? 0 : listnews.size();
	}

	@Override
	public ProductSubDetailBean getItem(int position) {
		// TODO Auto-generated method stub
		return listnews.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler holder;
		if (convertView == null) {
			holder = new ViewHodler();
			convertView = inflater.inflate(
					R.layout.product_sub_list_item_layout, null);
			holder.describeTv = (TextView) convertView
					.findViewById(R.id.product_sub_list_tv_describe);
			holder.mpriceTv = (TextView) convertView
					.findViewById(R.id.product_sub_list_tv_market_price);
			holder.priceTv = (TextView) convertView
					.findViewById(R.id.product_sub_list_tv_price);
			holder.numTv = (TextView) convertView
					.findViewById(R.id.product_sub_list_tv_sale_num);
			holder.picIv = (ImageView) convertView
					.findViewById(R.id.product_sub_list_iv_pic);
			convertView.setTag(holder);
		} else {
			holder = (ViewHodler) convertView.getTag();
		}
		final ProductSubDetailBean pbean = getItem(position);
		holder.describeTv.setText(pbean.getProducts_name());
		holder.mpriceTv.setText(getMprice(pbean.getMarket_price()));
		holder.priceTv.setText(getprice(pbean.getPrice()));
		holder.numTv.setText(getnum(pbean.getSales()));
		// final String url = pbean.getS_pic();
		// holder.picIv.setTag(url+ position);
		// String filePath = ConstantData.getImagePath(url);
		// Bitmap bitmap = ConstantData.getBitmapBypath(filePath);
		// holder.picIv.setImageBitmap(bitmap);
		// if (bitmap == null) {
		// holder.picIv.setImageResource(R.drawable.no_img_0);
		// load.loadDrawable(url, filePath);
		// }
		Log.e("mg", "p:" + pbean.getS_pic());
		ImageUtils.fb.display(holder.picIv, pbean.getS_pic());
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (iopen != null) {
					iopen.opentProductDetail(pbean);
				}
			}
		});

		return convertView;
	}

	class ViewHodler {
		TextView describeTv;
		TextView mpriceTv;
		TextView priceTv;
		TextView numTv;
		ImageView picIv;
	}

	@Override
	public void imageLoaded(String filePath, String imageUrl) {
		ImageView view = (ImageView) rootView.findViewWithTag(imageUrl);
		if (view != null) {
			view.setImageBitmap(ConstantData.getBitmapBypath(filePath));
		}
	}

	private String getMprice(String mprice) {
		return context.getResources().getString(R.string.sale_market_price,
				mprice);
	}

	private String getprice(String price) {
		return context.getResources().getString(R.string.sale_price, price);
	}

	private String getnum(String num) {
		return context.getResources().getString(R.string.sale_num, num);
	}
}
